package com.saint.easeuitest;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.saint.easeuitest.runtimepermissions.PermissionsManager;
import com.saint.easeuitest.runtimepermissions.PermissionsResultAction;

public class LoginActivity extends AppCompatActivity {
    private EditText userName_et, pwd_et;
    private Activity act;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        act = this;
        userName_et = findViewById(R.id.user_name_et);
        pwd_et = findViewById(R.id.pwd_et);

        getPermissions();

        if (EMClient.getInstance().isLoggedInBefore()){
            userName_et.setText(EMClient.getInstance().getCurrentUser());
        }

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                getLoginDate();
                break;
            default:
                break;
        }
    }

    private void getLoginDate() {
        String userName = userName_et.getText().toString().trim();
        String pwd = pwd_et.getText().toString().trim();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户名密码不可为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        EMClient.getInstance().login(userName, pwd, new EMCallBack() {
            @Override
            public void onSuccess() {
                startActivity(new Intent(act, MainActivity.class));
                act.finish();
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

    private void getPermissions() {
        /**
         * 请求所有必要的权限----原理就是获取清单文件中申请的权限
         */
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                Toast.makeText(act, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                Toast.makeText(act, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });

//        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,new String[]{Manifest.permission.CAMERA}, new PermissionsResultAction(){
//
//            @Override
//            public void onGranted() {
////                Toast.makeText(mContext, "拍照权限已获取", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onDenied(String permission) {
//                Toast.makeText(mContext, "Permission " + permission + " 获取失败", Toast.LENGTH_SHORT).show();
//            }
//        });
    }
}
